package com.huqu.beans.tintuc;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import com.huqu.beans.sessionWeb.SessionWeb;
import com.huqu.dao.N300Dao;
import com.huqu.entities.N300;
import com.huqu.entities.Page;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;

@ManagedBean(name = "kindNewsBean")
@ViewScoped
public class kindNewsBean implements Serializable {
	private static final long serialVersionUID = 6787137524912298632L;
	@ManagedProperty("#{sessionWeb}")
	private SessionWeb sessionWeb;
	private String vLang = "VN";
	private Page page = new Page();

	private String loaitintuc = "";
	private int idloaitintuc = 0;
	// la huong-nghiep= true; tuyen-sinh=false
	private boolean renderTitle = false;

	private List<N300> listN300 = new ArrayList<N300>();

	// ===================================================================================================================
	// =====================================================FUNCTIONS=====================================================
	@PostConstruct
	public void init() {
		try {
			System.out.println("====== kindNewsBean =======");
			page.setLimit(ApplicationConstant.LIMIT);

			if (ApplicationHelper.getRequest().getParameter(
					ApplicationConstant.PRETTY_PARA_LANG) != null) {
				vLang = ApplicationHelper.getRequest()
						.getParameter(ApplicationConstant.PRETTY_PARA_LANG)
						.toString().toUpperCase();

				if (vLang.trim().toUpperCase().equals("VN")) {
					if (sessionWeb.locale.equals(Locale.US)) {
						sessionWeb.setLocale(new Locale("vi", "VN"));
						FacesContext.getCurrentInstance().getViewRoot()
								.setLocale(new Locale("vi", "VN"));
						RequestContext.getCurrentInstance().execute(
								"location.reload();");
					}
				} else if (vLang.trim().toUpperCase().equals("EN")) {
					if (sessionWeb.locale.equals(new Locale("vi", "VN"))) {
						sessionWeb.setLocale(Locale.US);
						FacesContext.getCurrentInstance().getViewRoot()
								.setLocale(Locale.US);
						RequestContext.getCurrentInstance().execute(
								"location.reload();");
					}
				}
			} else {
				System.out.println("---language khong xac dinh ");
			}

			if (ApplicationHelper.getRequest().getParameter(
					ApplicationConstant.PRETTY_PARA_LOAITINTUC) != null) {
				loaitintuc = ApplicationHelper
						.getRequest()
						.getParameter(
								ApplicationConstant.PRETTY_PARA_LOAITINTUC)
						.trim().toLowerCase();
				if (loaitintuc.equals("huong-nghiep")) {
					idloaitintuc = 11;
					renderTitle = true;
				} else if (loaitintuc.equals("tuyen-sinh")) {
					idloaitintuc = 12;
					renderTitle = false;
				}
			}

			getListOfTabN300();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// load danh sách các câu hỏi
	public void getListOfTabN300() {
		try {
			N300Dao n300Dao = (N300Dao) ApplicationHelper
					.findBean(ApplicationConstant.REPOSITORY_NAME_N300);
			System.out.println("---ListOfTabN300( " + 0 + "," + idloaitintuc
					+ "," + page.getOffset() + "," + page.getLimit() + "," + 1
					+ "," + 1 + "," + 1 + "," + vLang + " )");
			listN300 = n300Dao.ListOfTabN300(0, idloaitintuc, page.getOffset(),
					page.getLimit(), 1, 1, 1, vLang);
			if (listN300.size() > 0) {
				page.setTotalpage(listN300.get(0).getTotalrow(),
						page.getLimit());
			} else {
				page.setTotalpage(0);
			}
			System.out.println("ListN300.size()= " + listN300.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// phân trang
	public void goToPage() {
		try {
			System.out.println("--- GoToPage( )");
			Map<String, String> params = FacesContext.getCurrentInstance()
					.getExternalContext().getRequestParameterMap();
			int numberPage = Integer.parseInt(params.get("numberpage"));
			page.setOffset((numberPage - 1) * page.getLimit());
			page.setCurrentpage(numberPage);
			System.out.println("--- numberpage= " + numberPage);
			getListOfTabN300();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SessionWeb getSessionWeb() {
		return sessionWeb;
	}

	public void setSessionWeb(SessionWeb sessionWeb) {
		this.sessionWeb = sessionWeb;
	}

	public String getvLang() {
		return vLang;
	}

	public void setvLang(String vLang) {
		this.vLang = vLang;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getLoaitintuc() {
		return loaitintuc;
	}

	public void setLoaitintuc(String loaitintuc) {
		this.loaitintuc = loaitintuc;
	}

	public int getIdloaitintuc() {
		return idloaitintuc;
	}

	public void setIdloaitintuc(int idloaitintuc) {
		this.idloaitintuc = idloaitintuc;
	}

	public boolean isRenderTitle() {
		return renderTitle;
	}

	public void setRenderTitle(boolean renderTitle) {
		this.renderTitle = renderTitle;
	}

	public List<N300> getListN300() {
		return listN300;
	}

	public void setListN300(List<N300> listN300) {
		this.listN300 = listN300;
	}
	
	
}
